# coding:utf-8
import os
import sys
import datetime
import set_env_path  # 请勿删除

from common.models import model_factory
from common.utils import DB


def update_relationship_status():
    """
    create by ld_wu 2018-05-28
    更新用户关系的状态
    通讯录关系状态
    邀请记录关系状态
    """

    MobileList = model_factory.mobile_lists
    InviteLog = model_factory.invite_logs

    with DB() as session:
        session.execute('''
            update
                invite_logs
            set status=25
            where status=20 and for_display=1
        ''')
        mobile_list = session.query(MobileList).filter(
            MobileList.deleted_at == 0,
            MobileList.status == 20
        ).all()

        invite_logs = session.query(InviteLog).all()

        invite_log_mapping = {
            '{}:{}'.format(each.follower_id, each.followed_id): each.status
            for each in invite_logs
        }

        friends = session.query(model_factory.follows).all()
        friend_mapping = {
            '{}:{}'.format(each.follower_id, each.followed_id)
            for each in friends
        }

        for each in invite_logs:
            key = '{}:{}'.format(each.followed_id, each.follower_id)
            if key not in invite_log_mapping:
                status = 25 if each.status == 20 else each.status
                if key in friend_mapping:
                    status = 30
                else:
                    # 已删除好友的情况
                    if status == 30:
                        status = 20
                # 下线邀请不做处理
                if each.source == 40:
                    continue
                InviteLog.create(
                    session,
                    followed_id=each.follower_id,
                    follower_id=each.followed_id,
                    status=status,
                    source=0,
                    read_status=True,
                    invite_time=each.invite_time
                )
                invite_log_mapping[key] = status
                print('new invite_logs: ', key, status)

            # 已删除对方
            if '{}:{}'.format(each.follower_id, each.followed_id) not in friend_mapping:
                if each.status == 30:
                    print('had del friend: ', each.follower_id, each.followed_id, each.status)
                    each.status = 20

        for each in mobile_list:
            status = invite_log_mapping.get(
                '{}:{}'.format(each.followed_id, each.follower_id), 0
            )
            print('mobile_list: ', each.follower_id, each.followed_id, each.status, status)
            each.status = status


def fix_duplicate_logs():
    with DB() as session:
        logs = session.execute(
            """
            select
                follower_id,
                followed_id
            from
                invite_logs
            
            group by
                follower_id,
                followed_id
            HAVING count(1) > 1
            """
        )

        for each in logs:
            session.execute(
                """
                delete
                from
                    invite_logs
                where
                    follower_id =:follower_id
                    and followed_id = :followed_id
                order by
                    invite_name asc limit 1;
                """,
                dict(
                    follower_id=each.follower_id,
                    followed_id=each.followed_id
                )
            )
            print('delete:', each.follower_id, each.followed_id)


if __name__ == '__main__':
    # fix_duplicate_logs()
    update_relationship_status()
